package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.CartForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * \* Author: Valley
 * \* Date: 2022/1/19
 * \* Time: 21:23
 * \* Description:
 */

@Slf4j
@RestController
@RequestMapping("shopping")
public class CartController {

    @Reference(timeout = 3000, retries = 2, check = false)
    ICartService cartService;


    /**
     * @description: 购物车列表
     * @param: [request]
     * @return: com.mall.commons.result.ResponseData
     * @author 王力
     * @date: 2022-01-20 09:46:35
     */
    @GetMapping("carts")
    public ResponseData cartList(HttpServletRequest request){
        String attribute = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject parse = (JSONObject) JSON.parse(attribute);
        Long uid = Long.parseLong(parse.get("uid").toString());

        CartListByIdRequest listByIdRequest = new CartListByIdRequest();
        listByIdRequest.setUserId(uid);

        CartListByIdResponse cartListById = cartService.getCartListById(listByIdRequest);
        if (cartListById == null){
            return new ResponseUtil().setErrorMsg("服务器繁忙");
        }

        if (!cartListById.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setErrorMsg(cartListById.getMsg());
        }

        return new ResponseUtil<>().setData(cartListById.getCartProductDtos());
    }

    /**
     * @description: 添加商品到购物车
     * @param: [userId, productId, productNum, request]
     * @return: com.mall.commons.result.ResponseData
     * @author 王力
     * @date: 2022-01-20 09:46:45
     */
    @PostMapping("carts")
    public ResponseData addCart(@RequestBody CartForm form, HttpServletRequest request){
        String attribute = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject parse = (JSONObject) JSON.parse(attribute);
        Long uid = Long.parseLong(parse.get("uid").toString());
        form.setUserId(uid);
        AddCartRequest addCartRequest = new AddCartRequest();

        addCartRequest.setUserId(form.getUserId());
        addCartRequest.setItemId(form.getProductId());
        addCartRequest.setNum(form.getProductNum());

        AddCartResponse addCartResponse = cartService.addToCart(addCartRequest);
        if (addCartResponse == null){
            return new ResponseUtil().setErrorMsg("服务器繁忙");
        }
        if (!addCartResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setErrorMsg(addCartResponse.getMsg());
        }

        return new ResponseUtil<>().setData("成功");
    }

    /**
     * @description: 更新购物车商品数量
     * @param: [cartForm, request]
     * @return: com.mall.commons.result.ResponseData
     * @author 王力
     * @date: 2022-01-20 09:47:01
     */
    @PutMapping("carts")
    public ResponseData updateCart(@RequestBody CartForm cartForm, HttpServletRequest request){
        String attribute = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject parse = (JSONObject) JSON.parse(attribute);
        Long uid = Long.parseLong(parse.get("uid").toString());
        cartForm.setUserId(uid);
        UpdateCartNumRequest cartNumRequest = new UpdateCartNumRequest();
        cartNumRequest.setChecked(cartForm.getChecked());
        cartNumRequest.setItemId(cartForm.getProductId());
        cartNumRequest.setUserId(cartForm.getUserId());
        cartNumRequest.setNum(cartForm.getProductNum());

        UpdateCartNumResponse updateCartNum = cartService.updateCartNum(cartNumRequest);
        if (updateCartNum == null){
            return new ResponseUtil().setErrorMsg("服务器繁忙");
        }
        if (!updateCartNum.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setErrorMsg(updateCartNum.getMsg());
        }
        return new ResponseUtil<>().setData("成功");
    }

    @DeleteMapping("/carts/{uid}/{pid}")
    /**
     * @description:  删除购物车中的商品
     * @author wangzhuo
     * @date 2022-01-20 17:23
     * @version 1.0
     */
   // public ResponseData deleteCartItem(@PathVariable("uid")Long userId,@PathVariable("pid")Long pid, HttpServletRequest request) {
    public ResponseData deleteCartItem(@PathVariable("pid")Long pid, HttpServletRequest request) {
        //不从request里面取userId,直接从Token里面取
        String attribute = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject parse = (JSONObject) JSON.parse(attribute);
        if(parse.get("uid").equals(null)){
            return new ResponseUtil<>().setData("传入参数为空");
        }
        Long uid = Long.parseLong(parse.get("uid").toString());

        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setItemId(pid);
       // deleteCartItemRequest.setUserId(userId);
        deleteCartItemRequest.setUserId(uid);
        cartService.deleteCartItem(deleteCartItemRequest);

        ResponseData responseData = new ResponseData();
        responseData.setCode(200);
        responseData.setMessage("success");
        responseData.setResult("删除购物车中的商品成功");
        responseData.setSuccess(true);
        return responseData;
    }
}
